from flask import request, abort

from models import User

from ..baseMethodView import BaseMethodView


# 用户登录，注册
class Login(BaseMethodView):
    def get(self):
        pass

    def post(self):
        # 获取客户端的参数
        args = request.json
        if args:
            account = args.get('account', None)
            md5_pwd = args.get('md5Pwd', None)
            if account and md5_pwd:
                # 查询用户
                user = User.query.filter(User.account == account).first()
                if user:
                    if user.MD5PWD == md5_pwd:
                        self.set_data(
                            dict(
                                flag=1,
                                token=user.token
                            )
                        )
                    else:
                        self.set_data(
                            dict(
                                flag=0
                            )
                        )
                else:
                    self.set_data(
                        dict(
                            flag=-1
                        )
                    )
                # 给客户端返回的数据
                return self.result()
            else:
                abort(403)
        else:
            abort(403)

    def put(self):
        pass

    def delete(self):
        pass
